package com.besall.allbase.view.activity.chipstoollevel4.protocol.Settings;

import static com.bes.sdk.message.Constants.SAVE_PANELS_JSON_FILE_SUCCESS;
import static com.besall.allbase.common.utils.ActivityUtils.showToast;

import android.bluetooth.BluetoothProfile;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bes.bessdk.service.base.BesServiceListener;
import com.bes.sdk.device.HmDevice;
import com.bes.sdk.utils.SettingsJsonUtils;
import com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants;
import com.besall.allbase.bluetooth.service.Protocol.ProtocolService;
import com.besall.allbase.view.activity.chipstoollevel4.protocol.ProtocolPresenter;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.view.dialog.CircularProgressDialog;

public class DeviceProfilesActivity extends BaseActivity implements BesServiceListener {
    private RecyclerView mRecyclerView;
    private DeviceProfilesAdapter mAdapter = null;

    private String device_model = "BC270";
    private String sn = "test";

    private CircularProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_profiles);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        List<String> list = new ArrayList<>();
        list.add(getString(R.string.device_profiles_setting0));
        list.add(getString(R.string.device_profiles_setting1));
        list.add(getString(R.string.device_profiles_setting2));
        list.add(getString(R.string.device_profiles_setting3));
        list.add(getString(R.string.device_profiles_setting4));
        mAdapter = new DeviceProfilesAdapter(list);
        mAdapter.setOnItemClickListener(position -> {
            Intent intent = new Intent(this, DeviceProfiles1Activity.class);
            intent.putExtra("sportMode", position);
            intent.putExtra("title", list.get(position));
            startActivity(intent);
        });
        mRecyclerView.setAdapter(mAdapter);
        // 获取码表的配置文件
        getPanelsJsonFileFromBesClient();
    }

    private void getPanelsJsonFileFromBesClient() {
        ProtocolPresenter protocolPresenter = ProtocolPresenter.getInstance();
        ProtocolService protocolService = protocolPresenter.getProtocolService();
        protocolPresenter.setProtocolServiceListener(this);
        if (null == protocolService) {
            showToast(R.string.pls_conn_device);
            return;
        } else {
            int state = protocolService.getDeviceConnectState();
            if (state != BluetoothProfile.STATE_CONNECTED) {
                showToast(R.string.pls_conn_device);
                return;
            }
        }
        if (progressDialog == null) {
            progressDialog = new CircularProgressDialog(this, false, false, null);
        }
        progressDialog.show(R.string.dialog_loading);
        device_model = SettingsJsonUtils.getDeviceModel(this);
        sn = SettingsJsonUtils.getSn(this);
        System.out.println("device_model:" + device_model);
        System.out.println("sn:" + sn);
        protocolPresenter.getBikeWatchDataFromBesClient(ProtocolConstants.BIKEWATCH_GET_PANELS_INFO,
                device_model, sn);
    }

    @Override
    public void onTotaConnectState(boolean state, HmDevice hmDevice) {
    }

    @Override
    public void onErrorMessage(int msg, HmDevice hmDevice) {
    }

    @Override
    public void onStateChangedMessage(int msg, String msgStr, HmDevice hmDevice) {
    }

    @Override
    public void onSuccessMessage(int msg, HmDevice hmDevice) {
        if (msg == SAVE_PANELS_JSON_FILE_SUCCESS) {
            if (progressDialog != null) {
                progressDialog.dismiss();
                progressDialog = null;
            }
        }
    }
}